/*
The MIT License (MIT)

Copyright (c) 2013 Mike Dapiran, Brian May, Richard Pospesel, and Bert Wierenga

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software 
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include "hhdTransform2D.h"
#include <cmath>
#include "hhdGUIPanel.h"


namespace hhd
{
	Transform2D::Transform2D()
	{
		_owner = NULL;

		m_translation_x = 0.0f;
		m_translation_y = 0.0f;

		m_x_base.x = 1.0f;
		m_x_base.y = 0.0f;

		m_scale_x = 1.0f;
		m_scale_y = 1.0f;

		m_world_matrix = new Matrix4();
		m_world_matrix->toIdentity();
	}

	Transform2D::~Transform2D()
	{
		delete m_world_matrix;
	}

	Transform2D::Transform2D(GUIPanel* in_owner)
	{
		_owner = in_owner;

		m_translation_x = 0.0f;
		m_translation_y = 0.0f;

		m_x_base.x = 1.0f;
		m_x_base.y = 0.0f;

		m_scale_x = 1.0f;
		m_scale_y = 1.0f;

		m_world_matrix = new Matrix4();
		m_world_matrix->toIdentity();
	}

	Transform2D::Transform2D(const Transform2D& in_other)
	{
		_owner = NULL;

		m_translation_x = in_other.m_translation_x;
		m_translation_y = in_other.m_translation_y;

		m_x_base = in_other.m_x_base;

		m_scale_x = in_other.m_scale_x;
		m_scale_y = in_other.m_scale_y;

		m_world_matrix = new Matrix4();
		m_world_matrix->toIdentity();
	}

	Transform2D& Transform2D::operator=(const Transform2D& in_other)
	{
		_owner = NULL;

		m_translation_x = in_other.m_translation_x;
		m_translation_y = in_other.m_translation_y;

		m_x_base = in_other.m_x_base;

		m_scale_x = in_other.m_scale_x;
		m_scale_y = in_other.m_scale_y;

		return *this;
	}


	void Transform2D::initialize(GUIPanel* in_panel)
	{
		_owner = in_panel;
		getWorldMatrix();
	}


	void Transform2D::setTranslation(float p_x, float p_y)
	{
		m_translation_x = p_x;
		m_translation_y = p_y;
	}

	Vector2 Transform2D::getTranslation()
	{
		return Vector2(m_translation_x, m_translation_y);
	}

	Vector2 Transform2D::getWorldTranslation()
	{
		Vector3 worldTranslation = getWorldMatrix().getTranslation();

		return Vector2(worldTranslation.x, worldTranslation.y);
	}


	void Transform2D::setXBase(float p_x_base_x, float p_x_base_y)
	{
		m_x_base.x = p_x_base_x;
		m_x_base.y = p_x_base_y;
		m_x_base.normalize();
	}

	void Transform2D::setYBase(float p_y_base_x, float p_y_base_y)
	{
		m_x_base.x = p_y_base_y;
		m_x_base.y = -p_y_base_x;
		m_x_base.normalize();
	}

	void Transform2D::setOrientation(float p_angle)
	{
		m_x_base.x = cos(p_angle);
		m_x_base.y = sin(p_angle);
		m_x_base.normalize();
	}

	Vector2 Transform2D::getXBase()
	{
		return m_x_base;
	}

	Vector2 Transform2D::getYBase()
	{
		return Vector2(m_x_base.y, -m_x_base.x);
	}

	float Transform2D::getOrientation()
	{
		return atan2(m_x_base.y + _EPSILON, m_x_base.x);
	}

	float Transform2D::getWorldOrientation()
	{
		return acos((*m_world_matrix)(0, 0));
	}


	void Transform2D::setScale(float p_x, float p_y)
	{
		m_scale_x = p_x;
		m_scale_y = p_y;
	}

	Vector2 Transform2D::getScale()
	{
		return Vector2(m_scale_x, m_scale_y);
	}


	void Transform2D::translate(float p_x, float p_y)
	{
		m_translation_x += p_x;
		m_translation_y += p_y;
	}

	void Transform2D::rotate(float p_angle)
	{
		float angle = getOrientation() + p_angle;
		setOrientation(angle);
	}

	void Transform2D::scale(float p_x, float p_y)
	{
		m_scale_x += p_x;
		m_scale_y += p_y;
	}

	const Matrix4& Transform2D::getWorldMatrix()
	{
		(*m_world_matrix)(0, 0) = m_scale_x * m_x_base.x;
		(*m_world_matrix)(0, 1) = m_scale_x * m_x_base.y;

		(*m_world_matrix)(1, 0) = -m_scale_y * m_x_base.y;
		(*m_world_matrix)(1, 1) = m_scale_y * m_x_base.x;

		(*m_world_matrix)(0, 3) = m_translation_x;
		(*m_world_matrix)(1, 3) = m_translation_y;


		if(!_owner || !_owner->getParentPanel())	
		{
			return *m_world_matrix;
		}
		else
		{
			(*m_world_matrix) = (_owner->getParentPanel()->getTransform2D().getWorldMatrix()) * (*m_world_matrix);
			return *m_world_matrix;
		}
	}


	Transform2D& Transform2D::copy(const Transform2D& in_other, GUIPanel* in_to_be_owner)
	{
		*this = in_other;
		_owner = in_to_be_owner;
		return *this;
	}
}